package com.xm.xmapp.dialog;

import java.util.ArrayList;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;

import com.xm.xmapp.R;
import com.xm.xmapp.adapter.ViewPagerAdapter;
import com.xm.xmapp.customview.MyViewPager;

public class GuideDialog extends Dialog {
	private Context context;

	public GuideDialog(Context context) {
		super(context, R.style.FenShiDialog);
		this.context = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		init();
	}

	private void init() {

		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_guide, null);
		setContentView(view);
		MyViewPager viewPager = (MyViewPager) view
				.findViewById(R.id.guide_dialog_viewpager);
		ArrayList<View> views = new ArrayList<View>();
		int[] imgResourse = { R.mipmap.guide1, R.mipmap.guide2,
				R.mipmap.guide3, R.mipmap.guide4 };

		int pad = (int) context.getResources().getDimension(R.dimen.dp15);
		for (int i = 0; i < 4; i++) {
			ImageView img = new ImageView(context);
			img.setImageResource(imgResourse[i]);
			img.setPadding(pad, 0, pad, 0);
			views.add(img);
		}
		viewPager.setAdapter(new ViewPagerAdapter(views));
		
		final ImageView imgArray[] = new ImageView[4];
		imgArray[0] = (ImageView) view.findViewById(R.id.img0);
		imgArray[1] = (ImageView) view.findViewById(R.id.img1);
		imgArray[2] = (ImageView) view.findViewById(R.id.img2);
		imgArray[3] = (ImageView) view.findViewById(R.id.img3);
		
		
		viewPager.addOnPageChangeListener(new MyViewPager.MyOnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				// TODO Auto-generated method stub
				for (int i = 0; i < imgArray.length; i++) {
					if (position == i) {
						imgArray[i].setImageResource(R.mipmap.haveseclect);
					} else {
						imgArray[i].setImageResource(R.mipmap.noseclect);
					}
				}
			}
			
		});
		
		view.findViewById(R.id.cha).setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
	
		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
		lp.width = (int) d.widthPixels; // 高度设置为屏幕的0.6
		lp.height = (int) d.heightPixels;
		dialogWindow.setAttributes(lp);
	}

}
